//
//  HomeActivityVC.m
//  jingdongfang
//
//  Created by LJL on 2018/5/8.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeActivityVC.h"
#import "HomeBannerCell.h"
#import "IGHomeReusableModel.h"
#import "HomeHDCell.h"
#import "HomeActivityCell.h"
#import "HomeReusableView.h"
#import "TopicVC.h"
#import "IGEmptyView.h"
#import "HomeTopicCollectionCell.h"
#import "HomeHDTypeVC.h"
#import "IGHomeActivityApi.h"
#import "IGPictureListDetailVC.h"
#import "IGActivityVC.h"
#import "HomeCommunityListVC.h"
#import "IGExhibitionVC.h"
#import "IGCompetitionVC.h"

@interface HomeActivityVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    NSString *hdTypeStr;
}
@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) NSMutableArray *reusableViewArr;
@property (nonatomic, strong) IGHomeActivityApi *api;
@property (nonatomic, strong) NSMutableArray *bannerArr;
@property (nonatomic, strong) NSMutableArray *hdArr;
@property (nonatomic, strong) NSMutableArray *activityArr;
@property (nonatomic, strong) NSMutableArray *topicArr;
@property (nonatomic, assign) HomeActivityType type;

@end

@implementation HomeActivityVC

-(UICollectionView *)collectionV
{
    if (!_collectionV)
    {
        _collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
        _collectionV.dataSource = self;
        _collectionV.delegate = self;
        _collectionV.backgroundColor = [UIColor c3Color];
        [_collectionV registerClass:[HomeBannerCell class] forCellWithReuseIdentifier:@"HomeBannerCell"];
        [_collectionV registerClass:[HomeReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeReusableView"];
        [_collectionV registerClass:[HomeHDCell class] forCellWithReuseIdentifier:@"HomeHDCell"];
        [_collectionV registerClass:[HomeTopicCollectionCell class] forCellWithReuseIdentifier:@"HomeTopicCollectionCell"];
        [_collectionV registerNib:[UINib nibWithNibName:@"HomeActivityCell" bundle:nil] forCellWithReuseIdentifier:@"HomeActivityCell"];
        __weak typeof(self) weakSelf = self;
        [_collectionV ig_setHeaderWithRefreshingBlock:^{
            [weakSelf dataRequest];
        }];
    }
    return _collectionV;
}

- (instancetype)initWithType:(HomeActivityType)type
{
    if (self = [super init])
    {
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = (_type == PhotographyType) ? NSLocalizedString(@"home7", nil) : NSLocalizedString(@"home8", nil);
    [self.view addSubview:self.collectionV];
    [self initBaseData];
    
    [self dataRequest];
    
    
}

- (void)initBaseData
{
    self.reusableViewArr = [@[] mutableCopy];
    NSArray *strArr = (_type == PhotographyType) ? @[NSLocalizedString(@"home9", nil) , NSLocalizedString(@"home3", nil) , NSLocalizedString(@"home10", nil) ] : @[NSLocalizedString(@"home11", nil) , NSLocalizedString(@"home3", nil) , NSLocalizedString(@"home10", nil)];
    for (int i = 0 ; i < 3 ; i++)
    {
        IGHomeReusableModel *model = [IGHomeReusableModel new];
        model.titleStr = strArr[i];
        CGFloat wid = [Tool widthForLabel:strArr[i] fontSize:18];
        model.titleWid = wid;
        model.type = normalType;
        [self.reusableViewArr addObject:model];
    }
    self.api = [[IGHomeActivityApi alloc] init];
    self.bannerArr = [@[] mutableCopy];
    self.hdArr = [@[] mutableCopy];
    self.activityArr = [@[] mutableCopy];
    self.topicArr = [@[] mutableCopy];
    hdTypeStr = @"";
}

- (void)dataRequest
{
    __weak typeof(self) weakSelf = self;
    self.api.type = (self.type == PhotographyType) ? @"2" : @"1";
    [self.api startWithDecodeClass:[IGHomeActivityResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [weakSelf.collectionV ig_endHeaderRefreshing];
        if (responseObject) {
            IGHomeActivityResp *resp = responseObject;
            IGHomeActivityModel *model = resp.data;
            weakSelf.bannerArr = [NSMutableArray arrayWithArray:model.banners];
            weakSelf.hdArr = [NSMutableArray arrayWithArray:model.drawList];
            weakSelf.activityArr = [NSMutableArray arrayWithArray:model.activitys];
            weakSelf.topicArr = [NSMutableArray arrayWithArray:model.circles];
            hdTypeStr = model.drawListLabelId;
            [weakSelf.collectionV reloadData];
        }
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return self.bannerArr.count ? 1 : 0;
    }
    else if (section == 1)
    {
        return self.hdArr.count ? : 0;
    }
    else if (section == 2)
    {
        return self.activityArr.count ? : 0;
    }
    else if (section == 3)
    {
        return self.topicArr.count ? 1 : 0;
    }
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return CGSizeMake(KSCREENWIDTH, BANNERHEIGHT) ;
    }
    else if (indexPath.section == 1)
    {
        return CGSizeMake((KSCREENWIDTH - 30) / 3, (KSCREENWIDTH - 30) / 3 + 38);
    }
    else if (indexPath.section == 2)
    {
        return CGSizeMake(KSCREENWIDTH, (KSCREENWIDTH - 14) * 410 / 724 + 102);
    }
    else if (indexPath.section == 3)
    {
        return CGSizeMake(KSCREENWIDTH, KSCREENWIDTH  * 202 / 750 + 80);
    }
    return CGSizeZero;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        HomeBannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeBannerCell" forIndexPath:indexPath];
        cell.bannerArr = self.bannerArr;
        return cell;
    }
    else if (indexPath.section == 1)
    {
        HomeHDCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeHDCell" forIndexPath:indexPath];
        IGDrawModel *hdModel = _hdArr[indexPath.row];
        cell.hdModel = hdModel;
        return cell;
    }
    else if (indexPath.section == 2)
    {
        HomeActivityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeActivityCell" forIndexPath:indexPath];
        IGHomeActivity *actModel = _activityArr[indexPath.row];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:actModel.image]];
        cell.titleL.text = actModel.title;
        cell.subTitleL.text = actModel.subTitle;
        return cell;
    }
    else if (indexPath.section == 3)
    {
        HomeTopicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTopicCollectionCell" forIndexPath:indexPath];
        cell.dataArr = self.topicArr;
        return cell;
    }
    return nil;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader)
    {
        if (indexPath.section >= 1)
        {
            IGHomeReusableModel *model = self.reusableViewArr[indexPath.section - 1];
            HomeReusableView *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeReusableView" forIndexPath:indexPath];
            reusableV.model = model;
            [reusableV setNeedsLayout];
            if (indexPath.section == 1)
            {
                reusableV.block = ^{
                    HomeHDTypeVC *vc = [[HomeHDTypeVC alloc] init];
                    vc.title = (_type == PhotographyType) ? NSLocalizedString(@"home7", nil) : NSLocalizedString(@"home8", nil);
                    vc.type = hdTypeStr;
                    [UIViewController ig_pushViewController:vc animated:YES];
                };
            }
            else if (indexPath.section == 2)
            {
                reusableV.block = ^{
                    IGActivityVC *activityVC = [[IGActivityVC alloc] init];
                    activityVC.type = self.type == PhotographyType?IGActivityListTypeSheYing:IGActivityListTypeShaoErYiShu;
                    [UIViewController ig_pushViewController:activityVC animated:YES];
                };
            }
            else if (indexPath.section == 3)
            {
                reusableV.block = ^{
                    [UIViewController ig_pushViewController:[[HomeCommunityListVC alloc] initWithType:_type] animated:YES];
                };
            }
            return reusableV;
        }
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 1 && self.hdArr.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 60);
    }
    else if (section == 2 && self.activityArr.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 60);
    }
    else if (section == 3 && self.topicArr.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 60);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 1)
    {
        return UIEdgeInsetsMake(0, 7.5, 18, 7.5);
    }
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 1)
    {
        return 18;
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 1)
    {
        return 7.5;
    }
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        IGDrawModel *hd = self.hdArr[indexPath.row];
        IGPictureListDetailVC *vc = [[IGPictureListDetailVC alloc] init];
        vc.drawlistId = hd.drawId;
        [UIViewController ig_pushViewController:vc animated:YES];
    } else if (indexPath.section == 2) {
        //活动推荐
        IGHomeActivity *activity = self.activityArr[indexPath.row];
        if ([activity.type isEqualToString:@"1"]) {
            IGExhibitionVC * vc = [IGExhibitionVC new];
            vc.activityId = activity.aid;
            [UIViewController ig_pushViewController:vc animated:YES];
        } else if ([activity.type isEqualToString:@"2"]) {
            IGCompetitionVC * vc = [IGCompetitionVC new];
            vc.activityId = activity.aid;
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
